function v = bvx_GetVar(hfile, varname)
% BVX::GetVar  - retrieve named variable from BVX object
%
% FORMAT:       v = bvx.GetVar(varname);
%
% Input fields:
%
%       varname     variable name
%
% Output fields:
%
%       v           retrieved variable content

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% check arguments
if nargin < 2 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'bvx') || ...
   ~ischar(varname) || ...
    isempty(varname)
    error( ...
        'BVQXfile:BadArgument', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end

% get content
bc = bvqxfile_getcont(hfile.L);

% try to locate variable
vpos = find(strcmp({bc.Variables(:).Name}, varname(:)'));
if isempty(vpos)
    error( ...
        'BVQXfile:NotFound', ...
        'Variable %s not found.', ...
        varname(:)' ...
    );
end

% return variable
v = bc.Variables(vpos(1)).Content;
